package com.uzai.console.service.sysplugin.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.ResponseMsg;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.sysplugin.SysPluginAddDto;
import com.uzai.console.dto.sysplugin.SysPluginIdDto;
import com.uzai.console.dto.sysplugin.SysPluginQuery;
import com.uzai.console.dto.sysplugin.SysPluginUpdateDto;
import com.uzai.console.entity.SysPlugin;
import com.uzai.console.mapper.SysPluginMapper;
import com.uzai.console.service.sysplugin.SysPluginService;
import com.uzai.console.vo.sysplugin.SysPluginVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/***
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
public class SysPluginServiceImpl implements SysPluginService {

    @Autowired
    private SysPluginMapper sysPluginMapper;

    /**
     * 查询插件列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param sysPluginQuery
     * @return null
     */
    public List<SysPluginVo> findSysPluginList(SysPluginQuery sysPluginQuery){
        return sysPluginMapper.findByList(sysPluginQuery);
    }

    /**
     * 查询插件详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param sysPluginIdDto
     * @return null
     */
    public SysPluginVo findSyspluginInfo(SysPluginIdDto sysPluginIdDto){

        if(sysPluginIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        //查询插件信息
        SysPlugin SysPlugin = sysPluginMapper.selectById(sysPluginIdDto.getId(), sysPluginIdDto.getMerId());
        //返回页面列表
        SysPluginVo sysPluginVo = new SysPluginVo();
        //对象复制
        BeanUtils.copyProperties(SysPlugin, sysPluginVo);
        return sysPluginVo;
    }

    /**
     * 添加插件详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param sysPluginAddDto
     * @return null
     */
    public void addSysplugin(SysPluginAddDto sysPluginAddDto){

        if(sysPluginAddDto.getMerId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请先登录系统");
        }

        if(StringUtils.isBlank(Tools.getStr(sysPluginAddDto.getPlugName())) || StringUtils.isBlank(Tools.getStr(sysPluginAddDto.getData()))){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        Integer now = DateUtil.getNowTime();
        SysPlugin sysPlugin = new SysPlugin();
        sysPlugin.setId(IdWorker.getId());
        sysPlugin.setMerId(sysPluginAddDto.getMerId());
        sysPlugin.setPlugName(sysPluginAddDto.getPlugName());
        sysPlugin.setData(sysPluginAddDto.getData());
        sysPlugin.setCreateTime(now);
        sysPlugin.setUpdateTime(now);
        //保存数据库
        sysPluginMapper.insert(sysPlugin);
    }


    /**
     * 修改插件详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param sysPluginUpdateDto
     * @return null
     */
    public void updateSysplugin(SysPluginUpdateDto sysPluginUpdateDto){

        if(sysPluginUpdateDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        SysPlugin sysPlugin_old = sysPluginMapper.selectById(sysPluginUpdateDto.getId(), sysPluginUpdateDto.getMerId());
        if(sysPlugin_old == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该插件不存在");
        }
        //插件名称
        sysPlugin_old.setPlugName(sysPluginUpdateDto.getPlugName());
        //插件数据
        sysPlugin_old.setData(sysPluginUpdateDto.getData());
        //修改时间
        sysPlugin_old.setUpdateTime(DateUtil.getNowTime());
        //保存数据库
        sysPluginMapper.update(sysPlugin_old);
    }


    /**
     * 删除插件详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param sysPluginIdDto
     * @return null
     */
    public void deleteSysplugin(SysPluginIdDto sysPluginIdDto){

        if(sysPluginIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        //保存数据库
        sysPluginMapper.deleteById(sysPluginIdDto.getId(), sysPluginIdDto.getMerId());
    }

}
